﻿using MySpiderMgr.Domain.Common.Dtos;
using MySpiderMgr.Domain.Common.Enums;
using MySpiderMgr.Infrastructure.BaseEntities;
using MySpiderMgr.Infrastructure.CommonEnums;
using System;
using System.Collections.Generic;
using System.Text;

namespace MySpiderMgr.Domain.Common
{
    /// <summary>
    /// 系统序列
    /// </summary>
    public class SysSequence : BaseEntity<int>
    {
        #region 系统资源Key
        /// <summary>
        /// 系统资源Key
        /// </summary>
        const string ResourceKey = "ResourceNoSequence";
        #endregion

        /// <summary>
        /// 名称
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// 格式 XX{{Date:yyyyMMdd}}{{value:x}}
        /// </summary>
        public string Format { get; set; }
        /// <summary>
        /// 当前值
        /// </summary>
        public int CurrentValue { get; set; }
        /// <summary>
        /// 起始值
        /// </summary>
        public int StartValue { get; set; }
        /// <summary>
        /// 增量
        /// </summary>
        public int Increment { get; set; }
        /// <summary>
        /// 是否需要循环
        /// </summary>
        public bool IsCycle { get; set; }
        /// <summary>
        /// 周期
        /// </summary>
        public CyclePeriod Period { get; set; }
        /// <summary>
        /// 当前周期值
        /// </summary>
        public string PeriodValue { get; set; }
        /// <summary>
        /// 备注信息
        /// </summary>
        public string Remarks { get; set; }

        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public static SysSequence Create(CreateSysSequenceDto dto)
        {
            return new SysSequence
            {
                Name = dto.Name,
                Flag = EntityFlag.Normal,
                CurrentValue = dto.StartValue ?? 0,
                Format = dto.Format,
                Increment = dto.Increment ?? 1,
                IsCycle = dto.IsCycle,
                Period = dto.Period,
                PeriodValue = dto.PeriodValue,
                Remarks = dto.Remarks,
                StartValue = dto.StartValue ?? 0
            };
        }
        /// <summary>
        /// 获取下一个序列号
        /// </summary>
        public static void SetNextNo(SysSequence Sequence)
        {

        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="dto"></param>
        public void Modify(ModifySysSequenceDto dto)
        {
            this.Format = dto.Format;
            this.Increment = dto.Increment ?? 1;
            this.IsCycle = dto.IsCycle;
            this.Name = dto.Name;
            this.Period = dto.Period;
            this.PeriodValue = dto.PeriodValue;
            this.Remarks = dto.Remarks;
            this.StartValue = dto.StartValue ?? 1;
        }
        /// <summary>
        /// 删除
        /// </summary>
        public void Delete()
        {
            this.Flag = EntityFlag.Deleted;
        }
    }

}
